<?php
namespace addons\cash\controller\api;

use addons\cash\model\Cash;
use addons\cash\model\CashAccount;
use addons\moneylog\model\MoneyLog;
use app\common\controller\Api;
use think\Db;

class Index extends Api
{

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }
    public function index()
    {
        $this->error("当前插件暂无前台页面");
    }

    /**
     * 获取提现配置
     */
    public function getConfig(){
        $config = get_addon_config('cash');
        $this->success('',$config);
    }

    /**
     * 提现申请
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function cash(){
        $way = $this->request->param('way',1);
        $money = $this->request->param('money');
        $where['type'] = $way;
        $where['user_id'] = $this->auth->id;
        $accountInfo = db('cash_account')->where($where)->find();
        if(empty($accountInfo)){
            $this->error('未绑定'.($way==1?'微信':'支付宝').'提现账号','',$way==1?'105':'106');
        }
        $data['account'] = $accountInfo['account'];
        $data['truename'] = $accountInfo['truename'];
        $user_info = $this->auth->getUserinfo();
        if(empty($user_info['money_password']))$this->error('请先设置资金密码','','107');
        $config = get_addon_config('cash');
        $data['way'] = $way;
        $data['money'] = $money;
        $data['fee'] = bcdiv(bcmul($money,$config['percent'],3),100,2);
        $data['true'] = bcsub($money,$data['fee'],2);
        $data['user_id'] = $user_info['id'];
        $model = new Cash();
        Db::startTrans();
        try{
            MoneyLog::moneyLog('-'.$money,$this->auth->id,'提现扣除',3);
            $ret = $model->allowField(true)->save($data);
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            $this->error('系统故障：'.$e->getMessage());
        }
        if($ret)$this->success('申请提交成功');
        $this->error('申请提交失败');
    }

    /**
     * 提现列表
     * @throws \think\exception\DbException
     */
    public function lists(){
        $limit = $this->request->param('limit',15);
        $status = $this->request->param('status');
        $model = new Cash();
        if($status || $status===0){
            $where['status'] = $status;
        }
        $where['user_id'] = $this->auth->id;
        $ret = $model->where($where)->order('create_time desc')->paginate($limit);
        $this->success('',$ret);
    }

    /**
     * 绑定更新提现账号
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function setAccount(){
        $type = $this->request->param('type');
        $account = $this->request->param('account');
        $truename = $this->request->param('truename');
        if(empty($type))$this->error('请选择绑定账号类型');
        if(empty($account) || empty($truename))$this->error('请填写账号和关联姓名');
        $model = new CashAccount();
        $accountInfo = $model->where('type',$type)->find();
        $data = [
            'user_id'   =>  $this->auth->id,
            'type'      =>  $type,
            'account'   =>  $account,
            'truename'  =>  $truename
        ];
        if(empty($accountInfo)){
            //新增
            $ret = $model->allowField(true)->save($data);
        }else{
            //更新
            $ret = $model->save($data,['type',$type]);
        }
        if($ret)$this->success('操作成功');
        $this->error('操作失败');
    }

}
